/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.catosist.run.business.gestorguia;

import com.catosist.run.dataaccess.domain.GuiaDao;
import com.catosist.run.dataaccess.model.GuiaVO;
import com.catosist.run.services.GuiaDTO;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author igorov
 */
public class GestorGuiaImpl implements GestorGuia{

    private GuiaDao guiaDao;

    public GuiaDao getGuiaDao() {
        return guiaDao;
    }

    public void setGuiaDao(GuiaDao guiaDao) {
        this.guiaDao = guiaDao;
    }

    public List<GuiaDTO> getByComprobanteId(int comprobanteId) {
        List<GuiaVO> guiaVOs = guiaDao.getByComprobanteId(comprobanteId);
        List<GuiaDTO> guiaDTOs = new LinkedList<GuiaDTO>();
        for (Iterator<GuiaVO> it = guiaVOs.iterator(); it.hasNext();) {
            GuiaVO guiaVO = it.next();
            guiaDTOs.add(new GuiaDTO( guiaVO.getId(),
                    guiaVO.getNumero(),
                    guiaVO.getDescripcion(),
                    guiaVO.getDestinatarionombre(),
                    guiaVO.getDestinatariodireccion(),
                    guiaVO.getRemitentenombre(),
                    guiaVO.getRemitentedireccion()));
        }
        return guiaDTOs;
    }

    public int getIdByNumero(String numero) {
        List<GuiaVO> guiaVOs = guiaDao.getByNumero(numero);
        if (guiaVOs.size() > 0)
            return guiaVOs.get(0).getId();
        else return 0;
    }

    public void deleteById(int i) {
        guiaDao.deleteById(i);
    }

    public void deleteByIndex(int index) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List getAll() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public GuiaDTO getById(int voId) {
        GuiaVO guiaVO = guiaDao.get(voId);
        GuiaDTO guiaDTO = new GuiaDTO();
        guiaDTO.setDescripcion(guiaVO.getDescripcion());
        guiaDTO.setDestinatariodireccion(guiaVO.getDestinatariodireccion());
        guiaDTO.setDestinatarionombre(guiaVO.getDestinatarionombre());
        guiaDTO.setNumero(guiaVO.getNumero());
        guiaDTO.setRemitentedireccion(guiaVO.getRemitentedireccion());
        guiaDTO.setRemitentenombre(guiaVO.getRemitentenombre());
        guiaDTO.setId(guiaVO.getId());
        return guiaDTO;
    }

    public Object getByIndex(int indice) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getIdAtIndex(int indice) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getIndexOf(int voId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void saveOrUpdate(Object o) {
        GuiaDTO guiaDTO = (GuiaDTO) o;
        GuiaVO guiaVO = new GuiaVO();

        if(guiaDTO.getId() != 0)
            guiaVO.setId(guiaDTO.getId());
        guiaVO.setDescripcion(guiaDTO.getDescripcion());
        guiaVO.setNumero(guiaDTO.getNumero());
        guiaVO.setRemitentenombre(guiaDTO.getRemitentenombre());
        guiaVO.setRemitentedireccion(guiaDTO.getRemitentedireccion());
        guiaVO.setDestinatarionombre(guiaDTO.getDestinatarionombre());
        guiaVO.setDestinatariodireccion(guiaDTO.getDestinatariodireccion());

        guiaDao.saveOrUpdate(guiaVO);
    }

//    public void saveOrUpdate(Object o) {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }

    public void updateListVo() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
